package com.example.home;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.example.home.view.HomeLeaderboardFragment;
import com.example.home.view.HomePreonlineFragment;
import com.example.home.view.HomeRecommendFragment;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class CollectionAdapter extends FragmentStateAdapter {
    List<Fragment> fragments=new ArrayList<>();
    List<Integer> fragmentNames=new ArrayList<>();

    public CollectionAdapter(@NonNull @NotNull FragmentActivity fragmentActivity) {
        super(fragmentActivity);
    }

    public CollectionAdapter(@NonNull @NotNull Fragment fragment) {
        super(fragment);
        init();
    }

    public CollectionAdapter(@NonNull @NotNull FragmentManager fragmentManager, @NonNull @NotNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
    }

    //预加载fragment
    private void init(){
        fragments.add(new HomeRecommendFragment());
        fragmentNames.add(R.string.home_recommend);
        fragments.add(new HomePreonlineFragment());
        fragmentNames.add(R.string.home_Comming_soon);
        fragments.add(new HomeLeaderboardFragment());
        fragmentNames.add(R.string.home_leaderboard);
    }

    @NonNull
    @NotNull
    @Override
    public Fragment createFragment(int position) {
        return fragments.get(position);
    }

    @Override
    public int getItemCount() {
        return fragments.size();
    }

    /**
     * 获取页面名称
     * @param position 位置
     * @return
     */
    public Integer getFragmentName(int position) {
        return fragmentNames.get(position);
    }
}
